<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class Home_m extends Model {

    function Home_m() {
        parent::Model();
        $this->load->database();
    }

    function login($account_name, $pass) {
        $this->db->select('doggy_accounts.*,doggy_profile.display_name,doggy_profile.avatar as avatar');
        $this->db->from('doggy_accounts');
        $this->db->join('doggy_profile', 'doggy_accounts.id=doggy_profile.account_id', 'left');
        $this->db->where(array('doggy_accounts.account_name' => $account_name));
        $this->db->where(array('doggy_accounts.pass' => $pass));
        $query = $this->db->get();
        return $query;
    }

    function update_temp_pass($email, $data) {
        return $this->db->update('doggy_accounts', $data, array('email' => $email));
    }

    function get_exist_account($account_name) {
        $query = $this->db->get_where('doggy_accounts', array('account_name' => $account_name));
        return $query;
    }

    function get_exist_email($email) {
        $query = $this->db->get_where('doggy_accounts', array('email' => $email));
        return $query;
    }

    function get_account_infor_by_email($email) {
        $this->db->select('doggy_accounts.id,doggy_accounts.account_name,doggy_accounts.email');
        $this->db->from('doggy_accounts');
        $this->db->where('doggy_accounts.email', $email);
        return $this->db->get();
    }

    function register_account($data) {
        $query = $this->db->insert('doggy_accounts', $data);
        return $query;
    }

    function register_profile($data) {
        $query = $this->db->insert('doggy_profile', $data);
        return $query;
    }

    function get_account_id_max() {
        $this->db->select('MAX(id) as new_id');
        $query = $this->db->get('doggy_accounts');
        return $query;
    }

    function edit_profile($data, $id) {
        return $this->db->update('doggy_profile', $data, array('account_id' => $id));
    }

    function edit_account($data, $id) {
        return $this->db->update('doggy_accounts', $data, array('id' => $id));
    }

    function change_pass($data, $id) {
        return $this->db->update('doggy_accounts', $data, array('id' => $id));
    }

    function get_account_details($id) {
        $this->db->select('doggy_accounts.account_name,
            doggy_profile.account_id,
            doggy_profile.fullname,
            doggy_profile.display_name,
            doggy_profile.birthday,
            doggy_profile.color,
            doggy_profile.sex,
            doggy_profile.avatar,
            doggy_profile.address,
            doggy_profile.mind_status,
            doggy_profile.self_description,
            doggy_accounts.race,
            doggy_accounts.region,
            doggy_race.race_name,
            animus_sub_region.name as region_name
            ');
        $this->db->from('doggy_accounts ,doggy_profile,doggy_race,animus_sub_region');
        $this->db->where('doggy_accounts.id = doggy_profile.account_id');
        $this->db->where('doggy_accounts.race = doggy_race.id');
        $this->db->where('doggy_accounts.region = animus_sub_region.id');
        $this->db->where('doggy_accounts.id', $id);
        return $this->db->get();
    }

    function is_password($pass, $id) {
        $query = $this->db->get_where('doggy_accounts', array('id' => $id, 'pass' => $pass));
        if ($query->num_rows() > 0) {
            return true;
        }
        return false;
    }

    function count_wall_messages($id, $post_to) {
        $this->db->select('doggy_wall_messages.id,doggy_wall_messages.content,doggy_wall_messages.image,doggy_wall_messages.posted_time,doggy_profile.display_name,doggy_wall_messages.posted_by,doggy_profile.avatar');
        $this->db->from('doggy_wall_messages,doggy_profile');
        $this->db->where('doggy_wall_messages.posted_by = doggy_profile.account_id');
        $this->db->where('doggy_wall_messages.posted_by IN (Select doggy_friends.friend_id FROM doggy_friends WHERE doggy_friends.account_id=' . $id . ')' . ' or doggy_wall_messages.post_to = ' . $post_to);
        $query = $this->db->get();
        return count($query->result_array());
    }

    function show_wall_message($id, $post_to, $per_page, $off_set) {
        if ($id > 0) {
            $this->db->select('doggy_wall_messages.id,doggy_wall_messages.content,doggy_wall_messages.image,doggy_wall_messages.posted_time,doggy_profile.display_name,doggy_wall_messages.posted_by,doggy_wall_messages.post_to,doggy_profile.avatar');
            $this->db->from('doggy_wall_messages,doggy_profile');
            $this->db->where('doggy_wall_messages.posted_by = doggy_profile.account_id');
            $this->db->where('doggy_wall_messages.posted_by IN (Select doggy_friends.friend_id FROM doggy_friends WHERE doggy_friends.account_id=' . $id . ')' . ' or doggy_wall_messages.post_to = ' . $post_to);
            $this->db->order_by('doggy_wall_messages.posted_time', 'DESC');
            $this->db->limit($per_page, $off_set);
            $query = $this->db->get();
            return $query->result_array();
        } else {
            return array();
        }
    }

    function delete_wall_message($message_id) {
        return $this->db->delete('doggy_wall_messages', array('id' => $message_id));
    }

    function get_posted_user($id) {
        if ($id > 0) {
            $this->db->select('doggy_accounts.id, doggy_accounts.account_name');
            $this->db->from('doggy_accounts');
            $this->db->join('doggy_wall_messages', 'doggy_wall_messages.posted_by = doggy_accounts.id');
            $this->db->where('doggy_accounts.id', $id);
            $query = $this->db->get();
            return $query->result_array();
        } else {
            return array();
        }
    }

    function search($id, $sex, $race, $region, $per_page, $off_set) {
        if ($sex == '_')
            $sex = '';
        if ($race == '_')
            $race = '';
        if ($region == '_')
            $region = '';
        $this->db->select('doggy_accounts.id,doggy_accounts.account_name,doggy_profile.avatar');
        $this->db->from('doggy_accounts ,doggy_race ,animus_sub_region ,doggy_profile');
        $this->db->where('doggy_accounts.id = doggy_profile.account_id');
        $this->db->where('doggy_accounts.race = doggy_race.id ');
        $this->db->where('doggy_accounts.region = animus_sub_region.id');
        if ($sex != '') {
            $this->db->where('doggy_profile.sex', $sex);
        }
        if ($race != '') {
            $this->db->where('doggy_race.id', $race);
        }
        if ($region != '') {
            $this->db->where('animus_sub_region.id', $region);
        }
        $this->db->limit($per_page, $off_set);
        $query = $this->db->get();
        return $query->result_array();
    }

    function count_search($id, $sex, $race, $region) {
        if ($sex == '_')
            $sex = '';
        if ($race == '_')
            $race = '';
        if ($region == '_')
            $region = '';
        $this->db->select('doggy_accounts.id,doggy_accounts.account_name,doggy_profile.avatar');
        $this->db->from('doggy_accounts ,doggy_race ,animus_sub_region ,doggy_profile');
        $this->db->where('doggy_accounts.id = doggy_profile.account_id');
        $this->db->where('doggy_accounts.race = doggy_race.id ');
        $this->db->where('doggy_accounts.region = animus_sub_region.id');
        if ($sex != '') {
            $this->db->where('doggy_profile.sex', $sex);
        }
        if ($race != '') {
            $this->db->where('doggy_race.id', $race);
        }
        if ($region != '') {
            $this->db->where('animus_sub_region.id', $region);
        }
        $query = $this->db->get();
        return count($query->result_array());
    }

    function get_random_horoscope() {
        $this->db->select();
        $this->db->from('doggy_horoscope');
        $this->db->order_by('RAND()');
        $this->db->limit(2);
        $query = $this->db->get();
        return $query->result_array();
    }

    function detail_horoscope($id) {
        $this->db->select();
        $this->db->from('doggy_horoscope');
        $this->db->where('id', $id);
        $query = $this->db->get();
        return $query->result_array();
    }

    function list_horoscope() {
        $this->db->select();
        $this->db->from('doggy_horoscope');
        $query = $this->db->get();
        return $query->result_array();
    }

    //
    function get_random_parkway() {
        $this->db->select();
        $this->db->from('doggy_parkway');
        $this->db->order_by('RAND()');
        $this->db->limit(4);
        $query = $this->db->get();
        return $query->result_array();
    }

    function detail_parkway($id) {
        $this->db->select();
        $this->db->from('doggy_parkway');
        $this->db->where('id', $id);
        $query = $this->db->get();
        return $query->result_array();
    }

    function list_parkway() {
        $this->db->select();
        $this->db->from('doggy_parkway');
        $query = $this->db->get();
        return $query->result_array();
    }

    function get_random_member($per_page, $off_set, $is_random=FALSE) {
        $this->db->select('doggy_accounts.id,doggy_profile.display_name, doggy_accounts.account_name, doggy_profile.avatar,doggy_profile.birthday,doggy_race.race_name');
        $this->db->from('doggy_accounts,doggy_profile,doggy_race');
        $this->db->where('doggy_accounts.id = doggy_profile.account_id');
        $this->db->where('doggy_accounts.race = doggy_race.id');
        if ($is_random) {
            $this->db->order_by('RAND()');
        } else {
            $this->db->order_by('doggy_accounts.id', 'DESC');
        }
        $this->db->limit($per_page, $off_set);
        $query = $this->db->get();
        return $query->result_array();
    }

    function get_random_actualite() {
        $this->db->select();
        $this->db->from('lehu_actualite');
        $this->db->order_by('id DESC');
        $this->db->limit(1);
        $query = $this->db->get();
        return $query->result_array();
    }

    function count_all_message($id) {
        $this->db->select('doggy_wall_messages.id,doggy_wall_messages.content,doggy_wall_messages.image,doggy_wall_messages.posted_time,doggy_profile.display_name,doggy_wall_messages.posted_by,doggy_profile.avatar');
        $this->db->from('doggy_wall_messages,doggy_profile');
        $this->db->where('doggy_wall_messages.posted_by = doggy_profile.account_id');
        $this->db->where('doggy_wall_messages.posted_by', $id);
        $this->db->order_by('doggy_wall_messages.posted_time', 'DESC');
        $query = $this->db->get();
        return count($query->result_array());
    }

    function get_own_walls($id, $per_page, $off_set) {
        if ($id > 0) {
            $this->db->select('doggy_wall_messages.id,doggy_wall_messages.content,doggy_wall_messages.image,doggy_wall_messages.posted_time,doggy_profile.display_name,doggy_wall_messages.posted_by,doggy_profile.avatar');
            $this->db->from('doggy_wall_messages,doggy_profile');
            $this->db->where('doggy_wall_messages.posted_by = doggy_profile.account_id
                and (doggy_wall_messages.posted_by=' . $id . ' or doggy_wall_messages.post_to=' . $id . ')');
            $this->db->order_by('doggy_wall_messages.posted_time', 'DESC');
            $this->db->limit($per_page, $off_set);
            $query = $this->db->get();
            return $query->result_array();
        } else {
            return array();
        }
    }

    function get_images_message_by_id($message_id) {
        $this->db->select('doggy_wall_messages.image');
        $this->db->from('doggy_wall_messages');
        $this->db->where('doggy_wall_messages.id',$message_id);
        $query = $this->db->get();
        return $query;
    }

}

?>
